package com.zbkj.business.kaoshi;

import cn.hutool.core.date.DateUtil;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import com.tiangong.framework.util.SnowFlakeGenerateIdWorker;
import com.tiangong.framework.util.redis.RedisUtil;

import com.zbkj.business.ReturnEnum;
import com.zbkj.business.core.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description: 用户表
 * @create: 2022-11-26 20:36:34
 **/
@Service("user")
@Slf4j
public class UserService {

    @OpenApi
    public Return sendCode(CDO cdoRequest) {

        String phone = cdoRequest.getStringValue("phone");
        String s = RedisUtil.getInstance().get("PHONE:LIST");
        if(s.contains("|"+phone+"|")==false){
            return Return.valueOf(-1,"申请验证码无权限");
        }

        String code= RandomStringUtils.randomNumeric(6);
        Boolean bok = RedisUtil.getInstance().setNx(Constant.REDIS_LOCK_PHONECODE + phone,code, 60);
        /*if(!bok){
            return Return.valueOf(ReturnEnum.SYSTEM_PHONECODE);
        }*/
        RedisUtil.getInstance().set(Constant.REDIS_TEMP_PHONECODE + phone,code, 10 * 60L );
        CDO cdo=new CDO();
        cdo.set("授权码",code);
        return Return.valueOf(cdo);
    }

    @OpenApi
    public Return getbaimingdan(CDO cdoRequest) {
        String s = RedisUtil.getInstance().get("PHONE:LIST");
        CDO cdoData=new CDO();
        cdoData.setIfNotBlank("list",s);
        return Return.valueOf(cdoData);
    }
    @OpenApi
    public Return setbaimingdan(CDO cdoRequest) {
        RedisUtil.getInstance().set("PHONE:LIST",cdoRequest.getStringValue("baimingdan"));
        return Return.SUCCESS;
    }

    @OpenApi
    public Return login(CDO cdoRequest) {
        String phone = cdoRequest.getStringValue("phone");
        String s = RedisUtil.getInstance().get("PHONE:LIST");
        if(s.contains("|"+phone+"|")==false){
            return Return.valueOf(-1,"登入系统无权限");
        }
        //String phoneCode = cdoRequest.getStringValue("phoneCode");
       // String phoneCode = "147257";
        /*//手机验证码不正确
        String realPhoneCode = RedisUtil.getInstance().get(Constant.REDIS_TEMP_PHONECODE + phone);
        if( !phoneCode.equalsIgnoreCase("147257")){
            if (phoneCode.equalsIgnoreCase(realPhoneCode) == false) {
                return Return.valueOf(ReturnEnum.LOGIN_PHONECODE_ERROR);
            }
        }*/
        CDO cdo=new CDO();
        String newsalt = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        cdo.set("token",newsalt);
        RedisUtil.getInstance().set("TOEKN:"+newsalt,phone,60*120);
        return Return.valueOf(cdo);
    }


    @OpenApi
    public Return answerrender(CDO cdoRequest) {

        String token = cdoRequest.getStringValue("token");

        //手机验证码不正确
        String realPhoneCode = RedisUtil.getInstance().get("TOEKN:" + token);
        if (StringUtils.isBlank(realPhoneCode) ) {
            return Return.valueOf(ReturnEnum.LOGIN_ADMIN_LOGIN_TIMEOUT);
        }
       return Return.SUCCESS;
    }

    @OpenApi
    public Return answer(CDO cdoRequest) {

        String token = cdoRequest.getStringValue("token");
        String phone = RedisUtil.getInstance().get("TOEKN:" + token);
        if(StringUtils.isNotBlank(phone)){
            RedisUtil.getInstance().set("ANSWER:"+phone,cdoRequest.toString());
        }
        RedisUtil.getInstance().delete("TOEKN:" + token);
        return Return.SUCCESS;
    }

    @OpenApi
    public Return submit(CDO cdoRequest) {
        //获取今天的日期，格式为yyyyMMdd

        String today = DateUtil.format(new Date(), "yyyyMMdd");
        Long increment = RedisUtil.getInstance().increment("APPLY:" + today + ":COUNT", 1);
        RedisUtil.getInstance().set("APPLY:"+today+":"+increment,cdoRequest.toString());

        return Return.SUCCESS;
    }

    public static void main(String[] args) {
        String today = DateUtil.format(new Date(), "yyyyMMdd");
        System.out.println(today);
    }
}
